package java_thinking.unit_8._8_2;

/**
 * @Description : 多态
 * @author: cww
 * @DateTime: 2019-02-27 22:19
 */

import java.util.Random;

/**
 * 基类
 * 1.基类添加print方法，扩展类即可进行调用，
 *   如果扩展类没有进行覆写该方法，则使用的是父类的方法，反则用扩展类自己的覆写方法
 */
class Shape1 {
    public void draw(){}
    public void erase(){}
    public void print(){
        System.out.println("Shape.print");
    }
}

class Circle1 extends Shape1 {
    @Override
    public void draw(){
        System.out.println("Circle1.draw()");
    }
    @Override
    public void erase(){
        System.out.println("Circle1.erase()");
    }

    /**
     * 扩展类覆写了父类的print方法，则覆写后的内容
     */
    public void print(){
        System.out.println("Circle1.print()");
    }

}

class Square1 extends Shape1 {
    @Override
    public void draw(){
        System.out.println("Square1.draw()");
    }
    @Override
    public void erase(){
        System.out.println("Square1.erase()");
    }
}

class Triangle1 extends Shape1 {
    @Override
    public void draw(){
        System.out.println("Triangle1.draw()");
    }
    @Override
    public void erase(){
        System.out.println("Triangle1.erase()");
    }
}

class RandomShapeGenerator1 {
    private Random rand = new Random(47);
    public Shape1 getShape(){
        switch (rand.nextInt(3)){
            //return发生了向上转型
            case 0: return new Square1();
            case 1: return new Circle1();
            case 2: return new Triangle1();
            default: return new Square1();
        }
    }
}

class Shapes{
    private static RandomShapeGenerator1 generator = new RandomShapeGenerator1();

    public static void main(String[] args) {
        Shape1[] shape1s = new Shape1[9];
        for (int i = 0; i< shape1s.length; i++){
            shape1s[i] = generator.getShape();
        }
        for (Shape1 shape1 : shape1s){
            if (shape1 instanceof Circle1){
                System.out.println("this.is Circle1");
            }
            shape1.draw();
            shape1.print();

        }
    }
}